package com.ricchieri.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="scheduleCharter")
public class ScheduleCharter extends BaseEntity {
	private static final long serialVersionUID = -4496992792443131358L;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "schedule", nullable = false)
	@Cascade({org.hibernate.annotations.CascadeType.PERSIST,
	        org.hibernate.annotations.CascadeType.SAVE_UPDATE})
	private Schedule schedule;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "charter", nullable = true)
	private Charter charter;
	
	@Column(name="quantityReserved", nullable=false)
	private int quantityReserved = 0;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user", nullable = true)
	private User user;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "trip", nullable = false)
	private Trip trip;
	
	@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinColumn(name="scheduleCharter", nullable=false)
	private List<ScheduleCharterExpense> scheduleCharterExpense;
	
	@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="scheduleCharter")
	private List<Reservation> reservation;
	
	@Column(name="lastReportUpdate", nullable=true)
	private Date lastReportUpdate;
	
	@Column(name="lastTravelUpdate", nullable=true)
	private Date lastTravelUpdate;
	
	public Schedule getSchedule() {
		return schedule;
	}
	
	public void setSchedule(Schedule schedule) {
		this.schedule = schedule;
	}
	
	public Charter getCharter() {
		return charter;
	}
	
	public void setCharter(Charter charter) {
		this.charter = charter;
	}
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public Trip getTrip() {
		return trip;
	}
	
	public void setTrip(Trip trip) {
		this.trip = trip;
	}
	
	public List<ScheduleCharterExpense> getScheduleCharterExpense() {
		return scheduleCharterExpense;
	}
	
	public void setScheduleCharterExpense(List<ScheduleCharterExpense> expense) {
		this.scheduleCharterExpense = expense;
	}

	public int getQuantityReserved() {
		return quantityReserved;
	}

	public void setQuantityReserved(int quantityReserved) {
		this.quantityReserved = quantityReserved;
	}

	public List<Reservation> getReservation() {
		if(reservation == null) {
			reservation = new ArrayList<Reservation>();
		}
		return reservation;
	}

	public void setReservation(List<Reservation> reservation) {
		this.reservation = reservation;
	}
	
	public List<Reservation> getActiveReservations() {
		List<Reservation> activeReservation = new ArrayList<Reservation>();
		for (Reservation reser : reservation) {
			if(!reser.getCancel()) {
				activeReservation.add(reser);
			}
		}
		return activeReservation;
	}

	public Date getLastReportUpdate() {
		return lastReportUpdate;
	}

	public void setLastReportUpdate(Date lastReportUpdate) {
		this.lastReportUpdate = lastReportUpdate;
	}

	public Date getLastTravelUpdate() {
		return lastTravelUpdate;
	}

	public void setLastTravelUpdate(Date lastTravelUpdate) {
		this.lastTravelUpdate = lastTravelUpdate;
	}

}